package com.wnzt.baszh.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.wnzt.baszh.dao.IUtilDao;
import com.wnzt.baszh.entity.Tsysuser;
import com.wnzt.baszh.util.common.ConstantsUtils;


@Repository
public class UtilDaoImpl implements IUtilDao{

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public List<Map<String, Object>> getDept() {
		String sql = "select ftykh,fksname from tworkroom where ftype='2' order by fpx";
		 List<Map<String, Object>> list = this.jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public List<Map<String, Object>> getZzDoc() {
		String sql = "SELECT DISTINCT fzzdoct,fzzdoctbh from TPATIENTVISIT where FZZDOCTBH is not null and FZZDOCT is not null;";
		List<Map<String, Object>> list = this.jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public List<Map<String, Object>> getCategory() {
		String sql = "SELECT fcode,fname from SZH_BLLXMX";
		List<Map<String, Object>> list = this.jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public List<Map<String, Object>> getJB() {
		String sql = "select ficdrange,fname from tsdisease";
		List<Map<String, Object>> list = this.jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public List getBAXM() {
		String sql = "select * from BASZ_BAXM order by fpx asc";
		List<Map<String, Object>> list = this.jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public List queryRelation() {
		String sql = "select FZJC,FMC from TSTANDARDMX where FCODE='GBrelation'";
		List<Map<String, Object>> list = this.jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public List getPrintType() {
		String sql = "select fname,ffynrzjc from basz_bafl";
        List list = this.jdbcTemplate.queryForList(sql);
        return list;
	}

	@Override
	public List<Map<String, Object>> findDistricts(String fcytykh, String district) {
		StringBuffer sql = new StringBuffer("SELECT fbqdm, fbqmc FROM this_bqdmk AS b INNER JOIN thisksset AS t ON b.fbqdm = t.fhiskh where t.fkstype = '81'");
		if (fcytykh.length()>0 || fcytykh != null) {
		    String numStr = findChildrenDepartments(fcytykh);  // 查询子科室号
		    if (numStr.length()>0 || numStr != null) {
		        sql.append(" and t.ftykh in ('" + numStr + "')");
		    } 
		}
		
		if (district.length()>0 || district != null) {
		    district = district.replace("\"", "'");
		    sql.append(" and t.fhiskh in (" + district + ")");
		}
		return jdbcTemplate.queryForList(sql.toString()); 
	}
	
	@Override
    public List queryDepartment(Tsysuser user, String menuCode) {
        String group = user.getFgroupcode();
        String workroom = user.getFworkroom();
        
        String rightSql = "select frightcode from tgroupright where fgroupcode = ?";
        List<Map<String, Object>> rightList = jdbcTemplate.queryForList(rightSql, group);
        
        String rightCodes = "";
        for (int i = 0; i < rightList.size(); i++) {
            rightCodes += "'" + rightList.get(i).get("frightcode") + "',";
        }
        rightCodes = rightCodes.substring(0, rightCodes.length() - 1);
        
        // 查询当前用户页面内容权限
        String contentSql = "select fcontentright from trightmenu where frightcode in (" + rightCodes + ") and fmenucode = '" + menuCode + "'";
        List<Map<String, Object>> contentList = jdbcTemplate.queryForList(contentSql);
        
        boolean all = false;
        for (Map<String, Object> map : contentList) {
            String content = (String) map.get("fcontentright");
            
            // 判断是否查询医院全部科室
            if (null != content && !"".equals(content)) {
                char c = content.charAt(0);
                if ('1' == c) {
                    all = true;
                    break;
                }
            }
        }
        
        List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
        
        // 查询医院全部科室
        if (all) {
            String sql = "select ftykh, fksname, (select ftykh from tsysuser where ftyyhbh = ?) as num from tworkroom where ftype='2' order by fpx";
            list = jdbcTemplate.queryForList(sql, user.getFtyyhbh());
        } else {   // 查询当前登录用户的工作科室
            if (null != workroom && !"".equals(workroom)) {
                workroom = workroom.replace("\"", "'");
                String sql = "select ftykh, fksname, (select ftykh from tsysuser where ftyyhbh = '" + user.getFtyyhbh() +"') as num from tworkroom where ftype='2' and ftykh in(" + workroom + ") order by fpx";
                list = jdbcTemplate.queryForList(sql);
            }
        }
        return list;
    }

	@Override
    public String findChildrenDepartments(String depart) {
        String numStr = "";
        if (depart.length()>0 || depart != null) {
            String sql = "select fcal from tworkroom where ftykh='" + depart + "'";
            List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
            if (!list.isEmpty()) {
                Map<String, Object> map = list.get(0);
                if (map.containsKey("fcal")) {
                    String departNum = (String) map.get("fcal");
                    if (departNum.length()>0 || departNum != null) {
                        numStr = departNum.replaceAll(",", "','");
                    }
                }
            }
        }
        return numStr;
    }

    @Override
    public String findParamValue(String name) {
        String sql = "select fvalue from tparam where fcode = ?";
        return jdbcTemplate.queryForObject(sql, String.class, name);
    }

	@Override
	public boolean addPreviewRecord(String uuid, String fzyid, String ip,
			String DATE, String fprn, int ftimes, String fname, String fcydate,
			String username, String usercode, String fcydept) {
		if(ConstantsUtils.isNotBank(fzyid)){
			String sql  = "insert into szh_browserecord ( uuid, fzyid, ip, fdate, fprn, ftimes, fname, fcydate, fusername, fusercode, fcydept)"
					+ " values('"+uuid+"','"+fzyid+"','"+ip+"','"+DATE+"','"+fprn+"',"+ftimes+",'"+fname+"','"+fcydate+"','"+username+"','"+usercode+"','"+fcydept+"') ";
			this.jdbcTemplate.execute(sql);
			return true;
		}else{
			return false;
		}
	}

	@Override
	public Object getBtnPermision(Tsysuser user, String tabtext) {
		String querySql = "select fcode from tmenu where fsysflag = 3 and fname='"+tabtext+"'";
		String menufcode = jdbcTemplate.queryForObject(querySql, String.class);  //获取菜单编号
		
		String usercode = user.getFgroupcode();  //获取该用户的用户组编号
		String sql = "SELECT t.FCONTENTRIGHT FROM TRIGHTMENU t INNER JOIN TRIGHT a ON t.FRIGHTCODE = a.FCODE "
				+ "INNER JOIN TGROUPRIGHT b ON b.FRIGHTCODE = a.FCODE INNER JOIN TGROUP c ON c.FCODE = b.FGROUPCODE"
				+ " WHERE c.fcode='" + usercode + "' and t.FMENUCODE = '"+menufcode+"'";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public List<Map<String, Object>> getSqmd() {
		String sql = "select fjymdbh,fname from szh_jymd ";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}

	

}
